package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    //    图片上传七牛云功能
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        //获取上传文件的原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        //获取文件的后缀
        String extention = originalFilename.substring(originalFilename.lastIndexOf("."));

        //使用UUID创建文件名称加文件后缀
        String fileName = UUID.randomUUID().toString() + extention;
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            //图片上传成功后保存图片名称存入redis,基于redis的set集合存储
            resource.sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);          //图片上传失败
        } finally {
            //关流  释放资源
            if (resource != null) {
                resource.close();
            }
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);    //图片上传成功
    }

    /*//新增套餐功能
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {

        try {
            setmealService.add(setmeal, checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }*/


    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return setmealService.findPage(queryPageBean);
    }

    //根据套餐id来查询对应的套餐数据,编辑回显
    @RequestMapping("/findById")
    public Result findById(Integer setmealId) {
        try {
            Setmeal setmeal = setmealService.findOne(setmealId);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);  //查询成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);    //查询失败
        }
    }

    //根据套餐id查询关联的检查组数据,进行编辑页面回显
    @RequestMapping("/findcheckgroupIdsBysetmealId")
    public Result findcheckgroupIdsBysetmealId(Integer setmealId) {
        List<Integer> list = null;
        try {
            list = setmealService.findcheckgroupIdsBysetmealId(setmealId);
        } catch (Exception e) {
            e.printStackTrace();
            new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);    //查询套餐关联检查组失败
        }
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);  //查询套餐关联检查组成功
    }

    //编辑套餐功能
    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.edit(setmeal, checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

    //删除套餐功能
    @RequestMapping("/delete")
    public Result delete(Integer setmealId) {
        try {
            setmealService.delete(setmealId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    //查询所有的方法
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Setmeal> list = setmealService.findAll();
        if (list != null && list.size() != 0) {
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, list);
        }
        return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
    }
}
